#include <string>
#include <map>
#include "User.h"
#include "./../Room/room.h"
#include "./../Tools/rfile.h"
#include "./../Tools/utility.h"
#include "./../Command/command.h"
#include "./../Socket/rwSocket.h"

using namespace std;

User::User(string strUserName)
{
	init(strUserName);
}
User::~User()
{
	init("");
};

void User::init(string strUserName)
{
	UserData[Const.userName] = strUserName;
	if (strUserName == "")
	{
		UserData[ Const.nowRoomID ] = "";
	}
	else
	{
		//load user information
	}
}

void User::OutputMessage(string message)
{
	(UserProfile.Socket)->SendLine(message);
}

void User::KickOut()
{
	//UserProfile.Socket.Close();
	rwSocket *ss;
	ss = UserProfile.Socket;
	ss->Close();
	cout << "be kickout address is " << ss << endl;
	extern map<string, User*> AllUser;
	AllUser.erase(UserData[ Const.userName ]);
}

bool User::Auth()
{
    char txt[255];
    string strPWDinFile;
    bool blReturn;
    ifstream inFile;
    string user_filename = User::getUserFileFullPath();
    inFile.open((user_filename).c_str());
    if (!inFile) {
		blReturn = false;
    }else{
		while(!inFile.eof())
		{
			inFile.getline(txt,255,'\n');
		    if (txt[0] != '\0')
		    {
		        string strKey, strValue;
		        utility::splistString(txt, '=', &strKey, &strValue);
		        if (utility::string2upper(strKey) == "PASSWORD")
		        {
		            strPWDinFile = strValue;
		            break;
				}
		    }
		}
		if (UserProfile.passWord == strPWDinFile)
		{
			//Auth passed
			bool bLoadScusses = Load();
			if (UserData[ Const.nowRoomID ] == "" )
			{
				UserData[ Const.nowRoomID ] = "school.0001";
			}
			cmd = command();
			cmd.setUser(this);
			blReturn = true;
		}
		else
		{
			blReturn = false;
		}
    }
    inFile.close();
	return blReturn;
}

bool User::IsUserExist()
{
	return rfile::isExist(getUserFileFullPath());
}

bool User::Load()
{
    char txt[255];
    string strPWDinFile;
    bool blReturn;
    ifstream inFile;
    string user_filename = User::getUserFileFullPath();
    inFile.open((user_filename).c_str());
    if (!inFile) {
		blReturn = false;
    }else{
		while(!inFile.eof())
		{
			inFile.getline(txt,255,'\n');
		    if (txt[0] != '\0')
		    {
		        string strKey, strValue;
		        utility::splistString(txt, '=', &strKey, &strValue);
		        UserData[strKey] = strValue;
		    }
		}
		blReturn = false;
    }
    inFile.close();
}

bool User::Save()
{
	
    string strErrorMessage;
    string strBackFileName, strFileName;
    strFileName = getUserFileFullPath();

    try{
		ofstream ofs(strFileName.c_str());
		ofs << "UserName = " << UserData[ Const.userName ] << std::endl;
		ofs << "Password = " << UserData[ Const.passWord ]  << std::endl;
		ofs << "NowRoomId = " << UserData[ Const.nowRoomID ] << std::endl;
		ofs.close();
        return true;
    }catch(...){
        return false;
    }
}

string User::getUserFileFullPath()
{
	return "./UserFolder/" +  UserData[ Const.userName ] + "_profile.txt";
}

void User::CommandHandler(string strCmd)
{
	cmd.exec(strCmd);
}

void User::dump()
{
	map<string, string>::iterator it;
	for ( it=UserData.begin() ; it!= UserData.end(); it++ )
	{
		cout << it->first << " : " << it->second << endl;
	}
	
}
/*
void User::usurpUserControler(Socket *socket)
{
	//modify AllUser defined in main.cpp
	extern map<string, User*> AllUser;
	map<string, User*>::iterator itAllUser;
	itAllUser = AllUser.find(UserData[ Const.userName ]);
	if (itAllUser == AllUser.end())
	{
		//not exist in ALLUser
		//Old logined account has been logout.
		isLoginScussess = true;
		m.writelog("Old one already not here now. Don't need kickout old account.");
	}
	else
	{
		//need kickout old one.
		(*u).OutputMessage(message::GetNormalMessage(16));
		(*u).UserProfile.iLoginStatus = 6;
		m.writelog("another login");
	}
	
	
}
*/
